package com.iefgames.cs.manager;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.iefgames.cs.pople.Cs;

public final class CsManager {
	
	private static final Map<String, Cs> CSMap = new HashMap<String, Cs>();
	
	public static void add(String uuid, Cs cs) {
		CSMap.put(uuid, cs);
	}
	
	public static Cs get(String uuid) {
		return CSMap.get(uuid);
	}
	
	public static void remove(String uuid) {
		CSMap.remove(uuid);
	}
	
	public static String randomOnlineCSUuid() {
		Set keys = CSMap.keySet();
		if (keys.isEmpty()) {
			return null;
		}
		
//		int index = 1;
//		int random = CsMath.getRandom(1, keys.size());
//		
//		
//		Iterator iterator = keys.iterator();
//		
//		String uuid = null;
//		while (iterator.hasNext()) {
//			if (index == random) {
//				uuid = (String) iterator.next();
//			}
//		}
//		
//		return uuid;
		
		return (String) keys.iterator().next();
	}
	
	public static String getUuidBySessionId(String sessionId) {
		Collection collection = CSMap.values();
		if (collection.isEmpty()) {
			return null;
		}
		
		Iterator iterator = collection.iterator();
		Cs cs = null;
		while (iterator.hasNext()) {
			cs = (Cs) iterator.next();
			
			if (cs.getSessionId().equals(sessionId)) {
				return cs.getUuid();
			}
		}
		
		return null;
	}
	
	public static boolean isOnline(String uuid) {
		return CometManager.get(uuid) != null ? true : false;
	}
	
	private CsManager() {}
}
